package com.wframe.project.jnd.rest.util;

import javax.servlet.http.HttpSession;

import com.wframe.project.jnd.wx.filter.WechatFilter;
import org.apache.commons.lang.StringUtils;

import com.frame.database.JdataBean;
import com.wframe.framework.spring.SpringContextHolder;
import com.wframe.project.jnd.services.UserService;

import java.util.Objects;

public class WebSession {

    private static final String WBSESSION_NAME = "_wbsession_";

    public static void checkLogin(HttpSession session) throws ClientException {
        if (getWbUser(session) == null) throw new ClientException(CodeEnum.LoginOut);
    }

    public static boolean checkLoginStatus(HttpSession session) {
        if (getWbUser(session) == null) return false;
        else return true;
    }

    public static SessionUser getWbUser(HttpSession session) {
        return (SessionUser) session.getAttribute(WBSESSION_NAME);
    }

    public static void setWbUser(HttpSession session, SessionUser user) {
        session.setAttribute(WBSESSION_NAME, user);
    }

    /**
     * 判断当前用户是否已认证
     *
     * @param session
     * @return
     */
    public static boolean checkWbUserAuth(HttpSession session) {
        SessionUser user = getWbUser(session);
        if (user == null) return false;
        else {
            UserService uservice = SpringContextHolder.getBean(UserService.class);
            try {
                return uservice.checkUserAuth(user.getUserid());
            } catch (Exception e) {
                return false;
            }
        }
    }

    public static void removeWbUser(HttpSession session) {
        session.removeAttribute(WBSESSION_NAME);
        session.removeAttribute(WechatFilter.WECHAT_USER_KEY);
    }

    public static SessionUser getWxUser(HttpSession session, String openId) {
        SessionUser user = (SessionUser) getWbUser(session);
        if (user == null) {
            if (StringUtils.isBlank(openId)) return null;
            try {
                com.wframe.project.former.dataservices.user.UserService userService = com.wframe.project.former.dataservices.user.UserService.getIntance();
                JdataBean ubean = userService.getUserByOpenId(openId);
                if (ubean != null) {
                    user = new SessionUser();
                    user.setOpenId(openId);
                    user.setUserid(ubean.getString("id"));
                    String uname = ubean.getString("nickname");
                    if (StringUtils.isBlank(uname)) uname = ubean.getString("loginname");
                    user.setUsername(uname);
                    if (ubean.getInt("authstate") == 2) user.setAuth(true);
                    setWbUser(session, user);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return user;
    }

    public static boolean checkWbUserAuth(String userid) {
        UserService uservice = SpringContextHolder.getBean(UserService.class);
        return uservice.checkUserAuth(userid);
    }

    public static String checkWbUserStatus(HttpSession session) {
        SessionUser sessionUser = getWbUser(session);
        if (sessionUser == null) {
            return "redirect:/user/showlogin";
        }
        String userid = sessionUser.getUserid();
        if (!checkWbUserAuth(userid)) {
            return "redirect:/user/certification";
        }
        return "success";
    }

    public static String getUserId(HttpSession session) throws ClientException {
        SessionUser sessionUser = getWbUser(session);
        if (Objects.isNull(sessionUser)) {
            throw new ClientException(CodeEnum.LoginOut);
        }
        return sessionUser.getUserid();
    }

    public static String checkUserAuth(HttpSession session) throws ClientException {
        String userId = getUserId(session);
        if (!checkWbUserAuth(userId)) {
            throw new ClientException(CodeEnum.NoAuth);
        }
        return userId;
    }

}
